import path from 'path'; //这个path用到了上面安装的@types/node

import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import viteCompression from 'vite-plugin-compression';

//vitejs.dev/config/
export default defineConfig({
  plugins: [
    react(),
    {
      ...viteCompression(),
      apply: 'build',
    },
  ],
  //这里进行配置别名
  resolve: {
    alias: {
      '@': path.resolve('./src'), // @代替src
    },
  },
  // 代理
  server: {
    host: '0.0.0.0',
    port: 8080,
    open: true,
    proxy: {
      '/api': {
        target: '要代理的地址',
        changeOrigin: true,
        ws: true,
        rewrite: (path: string) => path.replace(/^\/api/, ''),
      },
    },
  },
  // 分包
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          react: ['react', 'react-dom', 'react-router-dom'],
        },
      },
    },
  },

  css: {
    preprocessorOptions: {
      scss: {
        additionalData: '@import "@/assets/styles/index.scss";',
      },
    },
  },
});

// export default ({ mode }) => {
//   console.log('mode', loadEnv(mode, process.cwd()).VITE_BASE_URL); //127.0.0.1:9000/api
//   return defineConfig({
//     plugins: [react()],
//     //这里进行配置别名
//     resolve: {
//       alias: {
//         '@': path.resolve('./src'), // @代替src
//       },
//     },
//     css: {
//       preprocessorOptions: {
//         scss: {
//           additionalData: '@import "@/assets/styles/index.scss";',
//         },
//       },
//     },
//   });
// };
